// JavaScript Document
$(document).ready(function() {
	var module_alert = $("#module").val(); //name of the module/functionality to display in alert box
	if (module_alert == "message") {
		$("#srt_insertDate").addClass("sortDsc");
		$("#hid_sort").val("descending,insertDate");
	}
	
	//Implement ignore functionality for contact us messages
	$("#edit-ignore").click(function()	{
		if (checkbox_values = getSelectedCheckBoxes(module_alert)) {
			//open the confirm dialog before deactivate
			openConfirmDialog("ignore", checkbox_values, 400, 125, 'Are you Sure?', 'Do you want to ignore the selected messages?', "bulk");			
		}
		//return without submitting the page
		return false;
	});
	
	//Implement archive functionality for contact us messages
	$("#edit-archive").click(function()	{
		if (checkbox_values = getSelectedCheckBoxes(module_alert)) {
			//open the confirm dialog before deactivate
			openConfirmDialog("archive", checkbox_values, 400, 125, 'Are you Sure?', 'Do you want to archive the selected messages?', "bulk");						
		}
		//return without submitting the page
		return false;
	});
	
	//Date picker for date search in message listing pages
	$('#edit-cont-search-date').datepicker();
	//Date picker for date search in subject listing page
	$('#edit-sub-search-date').datepicker();
	//Multiselect dropdown for subject search in message listing pages
	$('#edit-cont-search-subject').dropdownchecklist();
	//Multiselect dropdown for subject search in subject listing page
	$('#edit-sub-search-subject').dropdownchecklist();
	
	//Function to implement add/edit for subjects in contactus
	$("#edit-add-new").click(function(){
		addEditSubjectDialog('new', 600, 225, 'Add New Subject');
		return false;
	});
	
	//Function to hide or show the filter box
	$("#edit-deactivate").click(function()	{
		if (checkbox_values = getSelectedCheckBoxes(module_alert)) {
			//open the confirm dialog before deactivate
			openConfirmDialog("deactivate", checkbox_values, 400, 125, 'Are you Sure?', 'Do you want to deactivate the selected subjects?', "bulk");
		}
		//return without submitting the page
		return false;
	});

  $("#edit-activate").click(function()	{
		if (checkbox_values = getSelectedCheckBoxes(module_alert)) {
			//open the confirm dialog before deactivate
			openConfirmDialog("activate", checkbox_values, 400, 125, 'Are you Sure?', 'Do you want to activate the selected subjects?', "bulk");
		}
		//return without submitting the page
		return false;
	});
	
	//Function to hide or show the filter box
	$("#reorder_subjects").click(function()	{
		$("#moreactions").hide();
		openDialog("reordersubjects", 0, 745, 550, "Reorder Subjects");
		return false;
	});
	
	//Function to configure the jquery dropdown
	configure_dropdown();
});

//Function to configure the jquery dropdown
function configure_dropdown () {
	//enable select option for fancy dropdown
	if ($("#edit-apply").attr('actionitem') == "contactus" || $("#edit-apply").attr('actionitem') == "archived") {
		$("#edit-cont-search-subject").val("-1");
    //$(".ui-dropdownchecklist-wrapper").css("overflow", "hidden");
		$(".ui-dropdownchecklist").css("width", "115px");
		$(".ui-dropdownchecklist span").css("width", "95px");
		//fix the height of the dropdown
		//$(".ui-dropdownchecklist-dropcontainer").css("margin-left", "-8px");
	}
	else if ($("#edit-apply").attr('actionitem') == "manage.subjects") {
		$("#edit-sub-search-subject").val("-1");
		$(".ui-dropdownchecklist").css("width", "230px");
		$(".ui-dropdownchecklist span").css("width", "210px");
	}
	//fix the height of the dropdown
	$(".ui-dropdownchecklist-dropcontainer").css("height", "250px");
	//set default text as select in dropdown
	$(".ui-dropdownchecklist-wrapper .ui-dropdownchecklist-text").html("Select");
	$(".ui-dropdownchecklist-wrapper .ui-dropdownchecklist-text").css("padding-left", "4px");
	//uncheck all the check boxes on loading
	$(".ui-dropdownchecklist-item INPUT[type=checkbox]").each(function () {
		this.checked = false;
	});
	//Set the default text if the no item is selected
	$(document).click(function () {
		if ($(".ui-dropdownchecklist-dropcontainer-wrapper").css("top") != "-3300px") {
			if ($(".ui-dropdownchecklist-text").html() == "") {
					if ($("#edit-cont-search-subject")) { 
						$("#edit-cont-search-subject").val("-1");
					}
					if ($("#edit-sub-search-subject")) {
						$("#edit-sub-search-subject").val("-1");
					}
				$(".ui-dropdownchecklist-wrapper .ui-dropdownchecklist-text").html("Select");
				$(".ui-dropdownchecklist-wrapper .ui-dropdownchecklist-text").css("padding-left", "4px");
			}
		}
	});
}

//Function to implement the filter action for messages
function applyMessageFilterAction (pageno, locate) {
	//prepare the field values for filtering
	var search_message = $.trim($("#edit-cont-search-message").val());
	var search_subject = $("#edit-cont-search-subject").val();
	var search_from = $.trim($("#edit-cont-search-from").val());
	var search_date = $.trim($("#edit-cont-search-date").val());
	var search_status = $("#edit-cont-search-status").val();
	var hid_sort = $('#hid_sort').val();
	
	//page loading animation
	pagination_loading("page");
	
	$.ajax({
		type : "POST",
		url : baseurl+"contactus/actions/filter"+(locate=='archived'?'/archived':''),
		data : "search_message="+encodeURIComponent(search_message)+"&search_subject="+encodeURIComponent(search_subject)+"&search_from="+encodeURIComponent(search_from)+"&search_date="+search_date+"&search_status="+search_status+"&sorting="+hid_sort+(pageno>1?'&page_no='+pageno:''),
		async : true,
		cache : false,
		success : function (msg) {
			$("#listingResultset").html(msg);
			var limit = $('#hid_limit').val();
			var page = pageno;
			var counter = $('#hid_totalRec').val();
			//create pagination links through AJAX
			create_pagination_links(counter, page, limit, locate, 'applyMessageFilterAction');
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to reload the subject area combobox
function reload_subject_listbox () {
	$.ajax({
		type : "POST",
		url : baseurl+"contactus/subjects/reload",
		async : true,
		cache : false,
		success : function (msg) {
			$("#SearchSubjectFilter").html(msg);
			$('#edit-sub-search-subject').dropdownchecklist();
		},
		error : function (res) {
			handleRequestError(res);
		}
	});		
}

//Function to implement the filter action for subjects
function applySubjectsFilterAction (pageno, locate) {
	//prepare the field values for filtering
	var search_subject = $("#edit-sub-search-subject").val();
	var search_added_by = $.trim($("#edit-sub-search-added-by").val());
	var search_date = $.trim($("#edit-sub-search-date").val());
	var search_status = $("#edit-sub-subject-status").val();
	var hid_sort = $('#hid_sort').val();
	
	//page loading animation
	pagination_loading("page");
		
	$.ajax({
		type : "POST",
		url : baseurl+"contactus/subjects/actions"+(locate=='archived'?'/archived':(locate=='manage.subjects'?'/manage.subjects':'')),
		data : "search_subject="+encodeURIComponent(search_subject)+"&search_added_by="+encodeURIComponent(search_added_by)+"&search_date="+search_date+"&search_status="+search_status+"&sorting="+hid_sort+(pageno>1?'&page_no='+pageno:''),
		async : true,
		cache : false,
		success : function (msg) {
			$("#listingResultset").html(msg);
			var limit = $('#hid_limit').val();
			var page = pageno;
			var counter = $('#hid_totalRec').val();
			//create pagination links through AJAX
			create_pagination_links(counter, page, limit, locate, 'applySubjectsFilterAction');
		},
		error : function (res) {
			handleRequestError(res);
		}
	});	
}

//Function to create pagination links while filtering
function create_pagination_links (counter, page, limit, locate, fn) {
	var str = "";
	var to = page*limit;
	
	str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
	if (page > 1) {
		str += '<a href="javascript:void(0)" onclick = "'+fn+'('+(parseInt(page)-1)+',\''+locate+'\')" class="red"><< Previous</a>';
	}
	if (to < counter) {
		str += '<span class="red"> |</span><a href="javascript:void(0)" onclick = "'+fn+'('+(parseInt(page)+1)+',\''+locate+'\')" class="red">Next >></a>';
	}
	
	//No records found when no result
	if (counter <= 0) {
		str = '';
	}
	
	$("#pagingdivtop").html(str);
	$("#pagingdivbottom").html(str);	
}

//function to open dialog box for alert messages
function addEditSubjectDialog (id, width, height, title) {
	$("#edit_subID").val(id);
	var wid = width;
	var heit = height;
	var buttons = false;
	$('#dialog').dialog({
		autoOpen: false,
		width: wid,
		height: heit,
		bgiframe: true,
		resizable: false,
		title:title,
		modal: true,
		show: 'puff',
		close: function () {
			$(this).dialog('destroy');
		}
	});
	$('#dialog').dialog('open');
	var h = heit;
	if(buttons){
		h = heit-95;
	}else{
		h = heit-40;
	}
	$('#dialog').css({
		'overflow-y':'auto',
		'width': (wid-18)+'px',
		'height':h+'px'
	}).show();
	
  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');		
	addEditSubject(id);
}

//Function to get the individual testimonial to display in detail page
function addEditSubject (id) {
	$.ajax({
		type : "POST",
		url : baseurl+'contactus/subjects/actions/'+id,
		async : true,
		cache : false,
		success : function (msg) {
			$('#dialog').html(msg);
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to save subjects
function saveSubjects () {
	var id = $("#edit_subID").val();
	var sub_name = $.trim($("#edit-subject-name").val());
	var sub_desc = $("#edit-subject-description").val();
	var displayorder = $("#edit-subject-displayorder").val();
	
	//code to validate the fields
	var lines = 10;
	var str = "";
	if (sub_name == "") {
		str += "Please enter the subject name";
		lines+=18;
	}
	if (sub_name != "") {
		if(sub_name.length > 50) {
			if(str!="")
					str+= "<br/>";
			str += "Subject name should not be more than 50 characters";
			lines+=18;
		}
	}
	if (sub_desc != "") {
		if(sub_desc.length > 1000) {
			if(str!="")
					str+= "<br/>";
			str += "Description should not be more than 1000 characters";
			lines+=18;
		}
	}

	if (str != "") {
		resize_dialog(600,225+lines);
		styleStatusMessages(str, "statusMessage", "failure");
		$('#dialog').scrollTop(0);
		return;
	}
	
	//page loading animation
  var content = $('#dialog').html();
	$(".ui-dialog-buttonpane").hide();
  $('#dialog').html('<div style="padding-top:60px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');		
	
	$.ajax({
		type : "POST",
		url : baseurl+"contactus/subjects/actions/save",
		data : "subject_id="+id+"&sub_name="+encodeURIComponent(sub_name)+"&sub_desc="+encodeURIComponent(sub_desc)+"&displayorder="+displayorder,
		async : true,
		cache : false,
		success : function (msg) {
			if (msg == "success") {
				$("#dialog").dialog("destroy");
				if (id == "new") {
					var totalrec = parseInt($("#hid_totalRec").val())+1;					
					//reload the subject listing
					reloadListingData($("#hid_limit").val(), 1, "", totalrec, baseurl+"contactus/manage.subjects", "page");
				}
				else {
					var totalrec = parseInt($("#hid_totalRec").val());
					//reload the subject listing
					applySubjectsFilterAction($("#hid_pageno").val(), 'manage.subjects');
				}				
			}
			else if(msg=="not_unique"){
					$('#dialog').html(content);
					$("#edit-subject-name").val(sub_name);
					$("#edit-subject-description").val(sub_desc);
					resize_dialog(600, 225 + 30);
					styleStatusMessages("Subject name already exist", "statusMessage", "failure");
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//function to activate or deactivate subjects
function activateDeactivateSubjects (ok, fn, id, mode) {
	var content = $("#dialog").html();
	if (ok && ok != "detail") {
		//Page loading animation 
		$(".ui-dialog-buttonpane").remove();
		$('#dialog').html('<div style="padding-top: 10px; padding-left: 150px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	}
	else {
		//Page loading animation 
		$(".ui-dialog-buttonpane").remove();
		$('#dialog').html('<div style="padding-top: 25px; padding-left: 200px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	}
	
	//AJAX call to update the status of the messages
	$.ajax({
		type : "POST",
		url : baseurl+"contactus/subjects/actions/"+(fn == "activate" ? fn : "deactivate"),
		data : "checkbox_values="+id,
		async : true,
		cache : false,
		success : function (msg) {
			if (msg == "success") {
				//destroy the dialog window
				$(ok).dialog('destroy');
				
				if (fn == "deactivate" && mode == "single") {
					$("#Deactivate"+id).hide();
					$("#Activate"+id).show();
					$("#SubjectStatus"+id).html("Inactive");
				}
				else if (fn == "activate" && mode == "single") {
					$("#Deactivate"+id).show();
					$("#Activate"+id).hide();
					$("#SubjectStatus"+id).html("Active");
				}
				else if (fn == "deactivate" && mode == "bulk") {
					$('input[@name=checkBoxList]:checked').each(function(){
						$("#Deactivate"+($(this).val())).hide();
						$("#Activate"+($(this).val())).show();
						$("#SubjectStatus"+($(this).val())).html("Inactive");
						this.checked = false;
					});
					if ($("#checkAll")) {
						$("#checkAll").attr('checked', false);
					}			
				}else if (fn == "activate" && mode == "bulk") {
					$('input[@name=checkBoxList]:checked').each(function(){
						$("#Deactivate"+($(this).val())).hide();
						$("#Activate"+($(this).val())).show();
						$("#SubjectStatus"+($(this).val())).html("Active");
						this.checked = false;
					});
					if ($("#checkAll")) {
						$("#checkAll").attr('checked', false);
					}
				}
				
				if (ok == "detail") {
					$("#dialog").html(content);
					if (fn == "activate") {
						$("#deactivateSubjectBtn").val("Deactivate");
						$("#subjectStatus").val("Active");
						$("#statusCodePopup").html("Active");
					}
					else {
						$("#deactivateSubjectBtn").val("Activate");
						$("#subjectStatus").val("Inactive");
						$("#statusCodePopup").html("Inactive");
					}
					loadSubjectFunctions(id);
					$("#dialog").dialog("destroy");
          			openAlertDialog(300, 125, 'Information', 'Subject Status changed successfully');
				}
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to ignore the selected messages
function ignoreArchiveMessages (ok, fn, id, mode, location) {
	var cnt = 0;
	//Prepare the status message
	if (fn == "ignore") {
		var str = "Ignored";
	}
	else if (fn == "archive") {
		var str = "Archived";
	}
	
	if (!ok) {
		//Page loading animation 
		$(".ui-dialog-buttonpane").remove();
		$('#dialog').html('<div style="padding-top:50px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	}
	else {
		$(".ui-dialog-buttonpane").remove();
		$('#dialog').html('<div style="padding-top: 10px; padding-left: 150px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	}

	//AJAX call to update the status of the messages
	$.ajax({
		type : "POST",
		url : baseurl+"contactus/actions/"+((fn == "ignore") ? fn : "archive"),
		data : "checkbox_values="+id,
		async : true,
		cache : false,
		success : function (msg) {
			if (msg == "success") {
				//destroy the popup window
				$("#dialog").dialog("destroy");
				
				if (mode == "single") {
					$("#listingOperations"+id).html(str);
					$("#Activate"+id).show();
					if (location == "popup") {
						//removing the archive button after archived
						if (fn == "ignore") {
							$("#archive_button").remove();
							$("#ignore_button").remove();
						}
						else if (fn == "archive") {
							$("#archive_button").remove();
						}
					}
					cnt++;
				}
				else if (mode == "bulk") {
					$('input[@name=checkBoxList]:checked').each(function(){
						$("#listingOperations"+($(this).val())).html(str);
						this.checked = false;
						cnt++;
					});
					if ($("#checkAll")) {
						$("#checkAll").attr('checked', false);
					}			
				}
				//code to retail the same page after reloading
				var reduce = parseInt($("#hid_totalRec").val()) - cnt;	
				var limit = parseInt($("#hid_limit").val());
				var pageno = parseInt($("#hid_pageno").val());
				var mod = reduce % limit;
				if (mod == 0) {
					if (pageno > 1) {
						pageno = pageno - 1;
					}
					else {
						pageno = pageno;
					}
				}
				
				//reload the listing to apply the effect
				if ($("#viewPopup").val() == "archived") {
					reloadurl = baseurl+"contactus/archived";
				}
				else {
					reloadurl = baseurl+"contactus";
				}
				reloadListingData (10, pageno, "", reduce, reloadurl, "page");
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});	
}

//Function to get the individual message to display in detail page
function contactusMessageDetails (id) {
	$.ajax({
		type : "POST",
		url : baseurl+'contactus/details/'+id,
		async : true,
		cache : false,
		success : function (msg) {
			//Display the response in dialog window
			$('#dialog').html(msg);
			
			//enable the events for ignore and archive buttons
			if ($("#viewPopup").val() == "archived") {
				//removing the archive button archive message view
				$("#archive_button").remove();			
				//ignore message in detail page
				$("#messagePopUpIngore").click(function() {
					openConfirmBox(function(){	
						$("#alertdialog").dialog("destroy");
						ignoreArchiveMessages("", "ignore", id, "single", "popup");
					}, 300, 100, "Confirm Ignore", "Do you want to ignore this message?");
				});	
			}
			else {
				//ignore message in detail page
				$("#messagePopUpIngore").click(function() {
					openConfirmBox(function(){		
						$("#alertdialog").dialog("destroy");
						ignoreArchiveMessages("", "ignore", id, "single", "popup");
					}, 300, 100, "Confirm Ignore", "Do you want to ignore this message?");	
				});			
				//archive message in detail page
				$("#messagePopUpArchive").click(function() {
					openConfirmBox(function(){		
						$("#alertdialog").dialog("destroy");
						ignoreArchiveMessages("", "archive", id, "single", "popup");
					}, 300, 100, "Confirm Archive", "Do you want to archive this message?");	
				});
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to load subject functions
function loadSubjectFunctions (id) {
	//ignore message in detail page
	$("#deactivateSubjectBtn").click(function() {
		if ($("#subjectStatus").val() == "Active") {
			var status_str = "deactivate";
		}
		else {
			var status_str = "activate";
		}
		openConfirmBox(function(){		
			$("#alertdialog").dialog("destroy");
			activateDeactivateSubjects ("detail", status_str, id, "single");
		}, 300, 100, "Confirm "+status_str, "Do you want to "+status_str+" this subject?");	
	});			
	//manage auto reply in detail page
	$("#manageAutoReplyBtn").click(function() {
		reopen_dialog(750, 450, "Manage Auto Reply");
		manageautoresponse();																							
	});
	//edit message in detail page
	$("#EditSubjectBtn").click(function() {
		reopen_dialog(600, 225, "Edit Subject");
		addEditSubject(id);																							
	});	
}

//Function to get the individual message to display in detail page
function contactusSubjectDetails (id) {
	$("#edit_subID").val(id);
	$.ajax({
		type : "POST",
		url : baseurl+'contactus/subjects/actions/view/'+id,
		async : true,
		cache : false,
		success : function (msg) {
			$('#dialog').html(msg);
			loadSubjectFunctions(id);
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to call the auto reply dialog window for subjects
function autoReply(id){
	$('#edit_subID').val(id);
	openDialog("autoresponse", 0, 780, 410, "Manage Auto Reply");
}

var editor;
//Function to display the auto reply page
function manageautoresponse(){
	$.ajax({
		type : "POST",
		url : baseurl+'contactus/manage.autoresponse/'+$('#edit_subID').val(),
		async : true,
		cache : false,
		success : function (msg) {
			//diaply the returned html in the dialog window
			$('#dialog').html(msg);
			$('.ui-dialog').css("filter","");
			//add editor to the textarea
			if(!editor){
				editor = CKEDITOR.replace('edit-resp-response',{
					skin : 'v2',
					resize_enabled:false,
					enterMode: Number(2),
					removePlugins : 'elementspath,save,forms,font,format,smiley,maximize,showblocks,about,stylescombo,pagebreak,flash'
				});
			}else{
				if (CKEDITOR.instances['edit-resp-response']) {
					CKEDITOR.remove(CKEDITOR.instances['edit-resp-response']);
				}
				editor = CKEDITOR.replace( 'edit-resp-response',{
					skin : 'v2',
					resize_enabled:false,
					enterMode: Number(2),
					removePlugins : 'elementspath,save,forms,font,format,smiley,maximize,showblocks,about,stylescombo,pagebreak,flash'
				});
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to save auto reply for subjects
function saveautoreply () {
	var fname = $('#edit-resp-from-name').val();
	var email = $('#edit-resp-from-email').val();
	var reply = $('#edit-resp-reply-to').val();
	var local = "EN";
	var sub = $('#edit-resp-subject').val();
	var resp = editor.getData();
	var subjectID = $('#edit_subID').val();
	
	var str = "";
	if($.trim(fname)=="")
		str += "From Name should not be empty";
		
	if($.trim(email) == ""){
		if(str!="")
				str+= "<br/>";
		str += "From Email Address should not be empty";
	}
	if ($.trim(email) != "") {
		if (!validateEmail(email)) {
			if(str!="")
					str+= "<br/>";
			str += "From Email Address is an invalid email address";
		}
	}
	if($.trim(reply)==""){
		if(str!="")
				str+= "<br/>";
		str += "Reply To should not be empty";
	}
	if ($.trim(reply) != "") {
		if (!validateEmail(reply)) {
			if(str!="")
					str+= "<br/>";
			str += "Reply To is an invalid email address";
		}
	}
	if($.trim(local)==""){
		if(str!="")
				str+= "<br/>";
		str += "Expired Date should not be empty";
	}
	if($.trim(sub)==""){
		if(str!="")
				str+= "<br/>";
		str += "Subject should not be empty";
	}
	if($.trim(resp)=="" || $.trim(resp)=="<br />"){
		if(str!="")
				str+= "<br/>";
		str += "Content should not be empty";
	}
	
	if (str != "") {
		styleStatusMessages(str, "statusMessage", "failure");
		$('#dialog').scrollTop(0);
		return;
	}
		
	$('#dialog').html('<div style="padding-top:130px; padding-left:345px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');		
	$.ajax({
		type : "POST",
		url : baseurl+'contactus/manage.autoresponse/save',
		data: 'fname='+fname+"&email="+email+"&reply="+reply+"&local="+local+"&subject="+sub+"&resp="+resp+"&subid="+subjectID,
		async : true,
		cache : false,
		success : function (msg) {
			if (msg=="Success") {
				$("#dialog").dialog("destroy");
        openAlertDialog(300, 125, 'Information', 'Auto reply message saved successfully');
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});
}

//Function to get the data for new message
function filterdata_contactus () {
	var data = getFilterdata("message");
	return data;   
}

//Function to get the data for archived message
function filterdata_archived () {
	var data = getFilterdata("archived");
	return data;   
}

//Function to get the filter field data for subjects
function filterdata_manage_subjects () {
	var search_subject = $("#edit-sub-search-subject").val();
	var search_added_by = $.trim($("#edit-sub-search-added-by").val());
	var search_date = $.trim($("#edit-sub-search-date").val());
	var search_status = $("#edit-sub-subject-status").val();
	var data = "&search_subject="+encodeURIComponent(search_subject)+"&search_added_by="+encodeURIComponent(search_added_by)+"&search_date="+search_date+"&search_status="+search_status;
	return data;
}

//Function to get the filter field data for new and archived messages
function getFilterdata (module) {
	//prepare the field values for filtering
	if (module == "message") {
		var search_message = $.trim($("#edit-cont-search-message").val());
		var mess = "&search_message="+search_message;
	}
	else {
		var mess = "";
	}
	var search_subject = $("#edit-cont-search-subject").val();
	var search_from = $.trim($("#edit-cont-search-from").val());
	var search_date = $.trim($("#edit-cont-search-date").val());
	var search_status = $("#edit-cont-search-status").val();

	filterdata = mess+"&search_subject="+search_subject+"&search_from="+search_from+"&search_date="+search_date+"&search_status="+search_status;
	return filterdata;
}

//function to reordering subjects
function reorderSubjects () {
	$('#dialog').html('<div style="padding-top:240px; padding-left:305px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	$.ajax({
		type : "POST",
		url : baseurl+'contactus/subjects/actions/list',
		async : true,
		cache : false,
		success : function (msg) {
			$('#dialog').html(msg);
				//Function call to reorder
				$(function() {
					$("#reorder ul").sortable({ opacity: 0.8, cursor: 'move', update: function() {
							$("#saveorder").show();
						}	
					});
				});
		},
		error : function (res) {
			handleRequestError(res);
		}
	});	
}

//Function to save the reordered list
function saveReorderedList () {
	var neworder = Array();
	$("#reorder .gridlister").each(function() {
		neworder.push($(this).attr("id").split("_")[1]);
	});
	$('#dialog').html('<div style="padding-top:240px; padding-left:305px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	$.ajax({
		type : "POST",
		data : "newOrder="+neworder,
		url : baseurl+'contactus/subjects/actions/save',
		async : true,
		cache : false,
		success : function (msg) {
			if (msg == "success") {
				$("#dialog").dialog("destroy");
				//reload the subject listing
				reloadListingData($("#hid_limit").val(), 1, "", parseInt($("#hid_totalRec").val()), baseurl+"contactus/manage.subjects", "page");
			}
		},
		error : function (res) {
			handleRequestError(res);
		}
	});	
}

//function to close the reorder dialog window
function closeReorderWindow () {
	$("#dialog").dialog("destroy");
}